package com.deyinet.bdcms.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.deyinet.bdcms.core.vo.Page;
import com.deyinet.bdcms.entity.Syslog;
import com.deyinet.bdcms.entity.Sysmodule;
import com.deyinet.bdcms.entity.Sysrole;
import com.deyinet.bdcms.entity.Sysuser;
import com.deyinet.bdcms.service.ILogsService;
import com.deyinet.bdcms.service.ISysuserService;
import com.deyinet.bdcms.tools.CheckNull;
import com.deyinet.bdcms.tools.MD5ArithmeticUtils;
import com.deyinet.bdcms.tools.WebUtilify;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/9/22 0022.
 */
@Controller
@RequestMapping(value = "/sysmanage/user")
public class SysUserController {
    @Autowired
    private ISysuserService moduleService;

    @RequestMapping(value = "/list")
    public void list() {
    }
    @Autowired
    private ILogsService logsService;
    @RequestMapping(value = "/add")
    public void add(String id, Model model) {
        model.addAttribute("id", id);
    }


    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> del(String json, HttpServletRequest request) {
        Syslog log =new Syslog();
        Sysmodule sysmodule=new Sysmodule();
        List<Sysuser> sysusers = JSONArray.parseArray(json, Sysuser.class);
        Integer[] idarr = new Integer[sysusers.size()];
        String content = "";
        for (int i = 0; i < sysusers.size(); i++) {
            idarr[i] = sysusers.get(i).getId();
            content+=idarr[i];
            if (i!=sysusers.size()-1){
                content+=",";
            }
        }
        moduleService.batchDelete(idarr);
        log.setTitle("删除用户");
        sysmodule.setId(9);//根据数据库sysmodule表的对应的id去set
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));
        log.setContent("删除id为"+content+"的用户");
        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        Map<String, Object> info = new HashMap<>();
        info.put("message", "删除成功");
        info.put("status", 200);
        info.put("closeCurrent", "false");
        return info;
    }

    @RequestMapping(value = "/listdata")
    @ResponseBody
    public Map<String, Object> listdata(final HttpServletRequest request, Page page) {
        String name = request.getParameter("name");
        Sysuser sysuser = new Sysuser();
        if (name != null && !"".equals(name)) {
            sysuser.setName(name);
        }
        moduleService.getPageByExampleLike(sysuser, page);
        Map<String, Object> info = new HashMap<>();
        info.put("totalRow", page.getTotalRow());
        info.put("pageCurrent", page.getPageCurrent());
        info.put("list", page.getList());
        return info;
    }

    @RequestMapping(value = "/getmodule", method = RequestMethod.POST)
    @ResponseBody
    public Sysuser getmodule(HttpServletRequest request, int id)
            throws Exception {
        Sysuser sysuser = moduleService.get(id);
        sysuser.setPassword("*****");
        return sysuser;
    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestBody Sysuser sysuser,HttpServletRequest request) {
        Map<String, Object> info = new HashMap<>();
        Syslog log =new Syslog();
        Sysmodule sysmodule=new Sysmodule();
        info.put("content", "操作失败");
        if (CheckNull.check(sysuser.getId())) {
            Sysuser old = moduleService.get(sysuser.getId());
            if (CheckNull.check(sysuser.getPassword()) && !"*****".equals(sysuser.getPassword())) {
                old.setPassword(MD5ArithmeticUtils.getMd5(sysuser.getPassword()));
            }
            old.setName(sysuser.getName());
            old.setMemo(sysuser.getMemo());
            old.setTel(sysuser.getTel());
            Sysrole role = new Sysrole();
            role.setId(sysuser.getRoleId());
            old.setSysroleByRoleid(role);
            moduleService.updateT(old);
            log.setTitle("修改用户");
            info.put("content", "修改成功");
            log.setContent("修改id为"+old.getId()+"的用户");
        } else {
            sysuser.setPassword(MD5ArithmeticUtils.getMd5(sysuser.getPassword()));
            sysuser.setCreatedate(new Timestamp(System.currentTimeMillis()));
            Sysrole role = new Sysrole();
            role.setId(sysuser.getRoleId());
            sysuser.setSysroleByRoleid(role);
            moduleService.insertT(sysuser);
            log.setTitle("添加用户");
            info.put("content", "添加成功");
            log.setContent("添加id为"+sysuser.getId()+"的用户");
        }
        sysmodule.setId(9);//根据数据库sysmodule表的对应的id去set
        log.setSysmoduleByModuleid(sysmodule);
        log.setSysuserByUserid(WebUtilify.LoginUserinfo(request));

        log.setCreatedate(new Timestamp(System.currentTimeMillis()));
        WebUtilify.WriteLog(request,log, logsService);
        info.put("Message", "ok");
        return info;
    }


}
